//
//  ZCViewController.swift
//  AirTravel
//
//  Created by apple on 2021/11/16.
//

import UIKit
import HandyJSON

class ZCViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate  ,UITextFieldDelegate{
    

    var type:Int = 0 //0.接送机 1.道路 2.租车

    @IBOutlet weak var subBt: UIButton!
    @IBOutlet weak var tab: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        tab.removeMoreLine()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func subClick(_ sender: Any) {
        
        switch type {
        case 0:
            var dic : [String:String] = [:]
            
            
            let cell = tab.cellForRow(at: IndexPath(row: 0, section: 0)) as! ZCOneTableViewCell
            let cell1 = tab.cellForRow(at: IndexPath(row: 1, section: 0)) as! ZCOneTableViewCell
            let cell2 = tab.cellForRow(at: IndexPath(row: 2, section: 0)) as! ZCOneTableViewCell
            let cell3 = tab.cellForRow(at: IndexPath(row: 3, section: 0)) as! ZCOneTableViewCell
            let cell4 = tab.cellForRow(at: IndexPath(row: 4, section: 0)) as! ZCOneTableViewCell
            let cell5 = tab.cellForRow(at: IndexPath(row: 5, section: 0)) as! ZCOneTableViewCell
            let cell6 = tab.cellForRow(at: IndexPath(row: 6, section: 0)) as! ZCTwoTableViewCell
            
            if(cell.content.text == ""){
                YProgressHUD.showToast("接机地点不能为空")
                return
            }
            if(cell1.content.text == ""){
                YProgressHUD.showToast("目的地不能为空")
                return
            }
            if(cell2.content.text == ""){
                YProgressHUD.showToast("联系人不能为空")
                return
            }
            if(cell3.content.text == ""){
                YProgressHUD.showToast("手机号不能为空")
                return
            }
            if(cell4.content.text == ""){
                YProgressHUD.showToast("人数不能为空")
                return
            }
            if(cell5.content.text == ""){
                YProgressHUD.showToast("预约时间不能为空")
                return
            }
            
            
            let str = stringToTimeStamp(stringTime: cell5.content.text!)

            print("\(str)")
     
            dic = ["address" : cell.content.text! , "destination" : cell1.content.text! ,"contact" : cell2.content.text! ,"mobile" : cell3.content.text! ,"number" : cell4.content.text! ,"order_time" : str ,"desc" : cell6.contentTF.text! ,]
            
            print("\(dic)")
            
            NetWorkRequest(API1.addAirportTransfer(parameters: dic), completion: {[weak self] (responseString) -> (Void) in
                guard let model = JSONDeserializer<NewsMainModel>.deserializeFrom(json: "\(responseString)") else {
                    return
                }
                
                YProgressHUD.showToast("提交成功")
                self?.navigationController?.popViewController(animated: true)
            }, failed: { (failedResutl) -> (Void) in
                print("服务器返回code不为0000啦~\(failedResutl)")
            }, errorResult: { () -> (Void) in
                print("网络异常,超时")
            })
            
        case 1:
            
            
            var dic : [String:String] = [:]
            
            
            let cell = tab.cellForRow(at: IndexPath(row: 0, section: 0)) as! ZCOneTableViewCell
            let cell1 = tab.cellForRow(at: IndexPath(row: 1, section: 0)) as! ZCOneTableViewCell
            let cell2 = tab.cellForRow(at: IndexPath(row: 2, section: 0)) as! ZCOneTableViewCell
            let cell3 = tab.cellForRow(at: IndexPath(row: 3, section: 0)) as! ZCTwoTableViewCell
     
            
            if(cell.content.text == ""){
                YProgressHUD.showToast("地址不能为空")
                return
            }
            if(cell1.content.text == ""){
                YProgressHUD.showToast("联系人不能为空")
                return
            }
            if(cell2.content.text == ""){
                YProgressHUD.showToast("手机号不能为空")
                return
            }
    

     
            dic = ["address" : cell.content.text! , "contact" : cell1.content.text! ,"mobile" : cell2.content.text! ,"desc" : cell3.contentTF.text! ]
            
            NetWorkRequest(API1.addRoadRescue(parameters: dic), completion: {[weak self] (responseString) -> (Void) in
                guard let model = JSONDeserializer<NewsMainModel>.deserializeFrom(json: "\(responseString)") else {
                    return
                }
                YProgressHUD.showToast("提交成功")
                self?.navigationController?.popViewController(animated: true)
            }, failed: { (failedResutl) -> (Void) in
                print("服务器返回code不为0000啦~\(failedResutl)")
            }, errorResult: { () -> (Void) in
                print("网络异常,超时")
            })
        default:
            
            
            var dic : [String:String] = [:]
            
            
            let cell = tab.cellForRow(at: IndexPath(row: 0, section: 0)) as! ZCOneTableViewCell
            let cell1 = tab.cellForRow(at: IndexPath(row: 1, section: 0)) as! ZCOneTableViewCell
            let cell2 = tab.cellForRow(at: IndexPath(row: 2, section: 0)) as! ZCOneTableViewCell
            let cell3 = tab.cellForRow(at: IndexPath(row: 3, section: 0)) as! ZCOneTableViewCell
            let cell4 = tab.cellForRow(at: IndexPath(row: 4, section: 0)) as! ZCOneTableViewCell
            let cell5 = tab.cellForRow(at: IndexPath(row: 5, section: 0)) as! ZCTwoTableViewCell
       
            
            
            
            if(cell.content.text == ""){
                YProgressHUD.showToast("地址不能为空")
                return
            }
            if(cell1.content.text == ""){
                YProgressHUD.showToast("联系人不能为空")
                return
            }
            if(cell2.content.text == ""){
                YProgressHUD.showToast("手机号不能为空")
                return
            }
            if(cell3.content.text == ""){
                YProgressHUD.showToast("品牌型号不能为空")
                return
            }
            if(cell4.content.text == ""){
                YProgressHUD.showToast("预约时间不能为空")
                return
            }
   
            let str = stringToTimeStamp(stringTime: cell4.content.text!)

            print("\(str)")
     
            dic = ["address" : cell.content.text! , "contact" : cell1.content.text! ,"mobile" : cell2.content.text! ,"model" : cell3.content.text! ,"order_time" : str ,"desc" : cell5.contentTF.text! ,]
            
            
            NetWorkRequest(API1.addCarRental(parameters: dic), completion: {[weak self] (responseString) -> (Void) in
                guard let model = JSONDeserializer<NewsMainModel>.deserializeFrom(json: "\(responseString)") else {
                    return
                }
                YProgressHUD.showToast("提交成功")
                self?.navigationController?.popViewController(animated: true)
            }, failed: { (failedResutl) -> (Void) in
                print("服务器返回code不为0000啦~\(failedResutl)")
            }, errorResult: { () -> (Void) in
                print("网络异常,超时")
            })
        }
        
    }
    
    func stringToTimeStamp(stringTime:String)->String {

        let dfmatter = DateFormatter()
        dfmatter.dateFormat="yyyy-MM-dd HH:mm:ss"
        let date = dfmatter.date(from: stringTime)
        
        let dateStamp:TimeInterval = date!.timeIntervalSince1970
        
        let dateSt:Int = Int(dateStamp)
        print(dateSt)
        return String(dateSt)
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        switch type  {
        case 0:
            switch indexPath.row {
            case 6:
                return 100
            default:
                return 50
            }
        case 1:
            switch indexPath.row {
            case 3:
                return 100
            default:
                return 50
            }
        default:
            switch indexPath.row {
            case 5:
                return 100
            default:
                return 50
            }
        }
        
        
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            switch type  {
                case 0:
                    return 7
                case 1:
                    return 4
                default:
                    return 6
            }
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        switch type {
        case 0:
            
            switch indexPath.row {

            case 0:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "接机地方"
                
                cell.content.placeholder = "选择接机地方"
                cell.content.tag = 1001
                cell.content.delegate = self
                cell.content.textAlignment = .right
                cell.flagIcon.isHidden = false
                return cell
            case 1:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "目的地"
                
                cell.content.tag = 1002
                cell.content.delegate = self
                cell.content.placeholder = "您在哪下车"
                cell.content.textAlignment = .right
                cell.flagIcon.isHidden = false
                return cell
                
            case 2:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "联系人"
                cell.content.placeholder = "输入联系人姓名"
                cell.content.textAlignment = .left
                cell.flagIcon.isHidden = true
                return cell
            case 3:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "联系电话"
                cell.content.placeholder = "输入联系电话"
                cell.content.textAlignment = .left
                cell.flagIcon.isHidden = true
                return cell
            case 4:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "人数"
                cell.content.placeholder = "选择人数"
                
                cell.content.tag = 1003
                cell.content.delegate = self
                cell.content.textAlignment = .right
                cell.flagIcon.isHidden = false
                return cell
            case 5:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                
                cell.title.text = "预约时间"
                cell.content.placeholder = "选择预约时间"
                cell.content.tag = 1007
                cell.content.delegate = self
                cell.content.textAlignment = .right
                cell.flagIcon.isHidden = false
                return cell
            default:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCTwoTableViewCell", for: indexPath) as! ZCTwoTableViewCell
                return cell
            }
            
        case 1:
            
            switch indexPath.row {

            case 0:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "地址"
                cell.content.tag = 1004
                cell.content.delegate = self
                cell.content.placeholder = "选择租车地址"
                cell.content.textAlignment = .right
                cell.flagIcon.isHidden = false
                return cell
            case 1:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "联系人"
                cell.content.placeholder = "输入联系人姓名"
                cell.content.textAlignment = .left
                cell.flagIcon.isHidden = true
                return cell
            case 2:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "联系电话"
                cell.content.placeholder = "输入联系电话"
                cell.content.textAlignment = .left
                cell.flagIcon.isHidden = true
                return cell

            default:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCTwoTableViewCell", for: indexPath) as! ZCTwoTableViewCell
                return cell
            }
            
        default:
            
            switch indexPath.row {

            case 0:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "地址"
                cell.content.tag = 1005
                cell.content.delegate = self
                cell.content.placeholder = "选择租车地址"
                cell.content.textAlignment = .right
                cell.flagIcon.isHidden = false
                return cell
            case 1:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "联系人"
                cell.content.placeholder = "输入联系人姓名"
                cell.content.textAlignment = .left
                cell.flagIcon.isHidden = true
                return cell
            case 2:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "联系电话"
                cell.content.placeholder = "输入联系电话"
                cell.content.textAlignment = .left
                cell.flagIcon.isHidden = true
                return cell
            case 3:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                cell.title.text = "品牌型号"
                cell.content.placeholder = "输入期待租车的品牌型号"
                cell.content.textAlignment = .left
                cell.flagIcon.isHidden = true
                return cell
            case 4:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCOneTableViewCell", for: indexPath) as! ZCOneTableViewCell
                
                cell.title.text = "预约时间"
                cell.content.tag = 1006
                cell.content.delegate = self
                cell.content.placeholder = "选择预约时间"
                cell.content.textAlignment = .right
                cell.flagIcon.isHidden = false
                return cell
            default:
                let cell = tableView.dequeueReusableCell(withIdentifier: "ZCTwoTableViewCell", for: indexPath) as! ZCTwoTableViewCell
                return cell
            }
            
        }
  
    }
    
    
    func showNumPerson(cell:ZCOneTableViewCell){
        
        BRStringPickerView.showPicker(withTitle: "人数", dataSourceArr: ["1" ,"2","5","10"], select: 0) { (model) in
       
            
            cell.content.text = model?.value
        }
        
    }
    
    func showTime(cell:ZCOneTableViewCell){
        BRDatePickerView.showDatePicker(with: .YMDHMS, title: "", selectValue: "") { (res, date) in
            cell.content.text = date
        }
    }
    
    
    func gotomap(cell:ZCOneTableViewCell){
        let vc = JXDMapViewController{(data) in
            cell.content.text = data
        }

        self.navigationController?.pushViewController(vc, animated: true)
    }


    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        
        switch textField.tag {
            case 1001:
       
                
                let cell:ZCOneTableViewCell = tab.cellForRow(at: [0,0]) as! ZCOneTableViewCell
                gotomap(cell: cell)
                return false
            case 1002:
         
                let cell:ZCOneTableViewCell = tab.cellForRow(at: [0,1]) as! ZCOneTableViewCell
                gotomap(cell: cell)
                return false
            case 1003:
             
                let cell:ZCOneTableViewCell = tab.cellForRow(at: [0,4]) as! ZCOneTableViewCell
                showNumPerson(cell: cell)
                return false
                
            case 1007:

            let cell:ZCOneTableViewCell = tab.cellForRow(at: [0,5]) as! ZCOneTableViewCell
            showTime(cell: cell)
            return false
            case 1004:
           
                
                let cell:ZCOneTableViewCell = tab.cellForRow(at: [0,0]) as! ZCOneTableViewCell
                gotomap(cell: cell)
                return false
            case 1005:
           
                let cell:ZCOneTableViewCell = tab.cellForRow(at: [0,0]) as! ZCOneTableViewCell
                gotomap(cell: cell)
                return false
            case 1006:
 
                let cell:ZCOneTableViewCell = tab.cellForRow(at: [0,4]) as! ZCOneTableViewCell
                showTime(cell: cell)
                return false

        default:
            return true
        }

        
    }
}
